﻿using Microsoft.AspNetCore.Mvc;

namespace Cache.Cache
{

    public static class CacheKeyHelper
    {

        /// <summary>
        /// 获取和这个Action请求相关的CacheKey,主要考虑Controller名字、Action名字、参数等
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static string CalcCacheKeyFromAction(this ControllerBase controller)
        {
            return GetCacheKey(controller.ControllerContext);
        }


        public static string GetCacheKey(this ControllerContext controllerContext)
        {
            #region Action.Controller方式组装key
            //ICollection<Object?> routeValues = controllerContext.RouteData.Values.Values;
            //Action.Controller
            //string cacheKey = string.Join(".", routeValues);
            #endregion

            #region Controller.Action方式组装key
            string? controller = controllerContext.RouteData.Values["controller"]?.ToString();
            string? action = controllerContext.RouteData.Values["action"]?.ToString();
            string cacheKey = string.Join(".",controller,action);
            #endregion

            return cacheKey;
        }
    }

}

